
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_wan_android/application.dart';
import 'package:flutter_wan_android/event/event.dart';
import 'package:flutter_wan_android/event/event_bus.dart';
import 'package:flutter_wan_android/res/colors.dart';
import 'package:flutter_wan_android/res/theme.dart';
import 'package:flutter_wan_android/routes/route_util.dart';
import 'package:flutter_wan_android/routes/routes.dart';
import 'package:image_picker/image_picker.dart';

import '../r.dart';

class MinePage extends StatefulWidget {
  @override
  _MinePageState createState() => _MinePageState();
}

class _MinePageState extends State<MinePage> {

  late File _image;
  final picker = ImagePicker();

  var userName = Application.userInfo?.nickname ?? "登录";

  @override
  void initState() {
    super.initState();

    EventBusUtil.getInstance().on<String>().listen((event) {
      if(event == WEvent.EVENT_LOGIN){
        setState(() {
          userName = Application.userInfo?.nickname ?? "登录";
        });
      }
    });
  }

  @override
  void dispose() {
    super.dispose();
    EventBusUtil.getInstance().destroy();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: wTheme.background,
      child: Column(
        children: [
          Container(
            height: 220,
            width: double.infinity,
            color: wTheme.mineHead,
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Padding(padding: EdgeInsets.only(top: 40)),
                GestureDetector(
                  onTap: (){
                    getImage();
                  },
                  child: Image.asset(
                    R.imagesIcHead,
                    width: 80,
                    height: 80,
                  ),
                ),
                Padding(padding: EdgeInsets.only(top: 10)),
                GestureDetector(
                  onTap: (){
                    if(!Application.hasLogin){
                      RouteUtil.toLoginPage();
                    }
                  },

                  child: Text(
                    Application.userInfo?.nickname ?? "登录",
                    style: TextStyle(
                      color: WColor.white,
                      fontSize: 18,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                ),
              ],
            ),
          ),
          _getItems(
            "我的积分",
            R.imagesIcJifeng,
                () {
              RouteUtil.push(context, Routes.myPoint, checkLogin: true);
            },
          ),
          _getItems(
            "我的收藏",
            R.imagesIcCollectFill,
                () {
              RouteUtil.push(context, Routes.myCollect, checkLogin: true);
            },
          ),
          // _getItems(
          //   "待办事项",
          //   R.imagesIcTodo,
          //       () {
          //     RouteUtil.push(context, Routes.todo);
          //   },
          // ),

          _getItems(
            "关于项目",
            R.imagesIcAbout,
             () {
              RouteUtil.push(
                context,
                Routes.web,
                params: {"url": "https://gitee.com/teprinciple/flutter_wan_android", "title": "WanAndroid Flutter"},
              );
            },
          ),

          _getItems(
            "系统设置",
            R.imagesIcSetting,
                () {
              RouteUtil.push(context, Routes.setting);
            },
          )
        ],
      ),
    );
  }

  Future getImage() async {
    final pickedFile = await picker.getImage(source: ImageSource.gallery);

    setState(() {
      if (pickedFile != null) {
        _image = File(pickedFile.path);
      } else {
        print('No image selected.');
      }
    });
  }

  _getItems(String title, String img, GestureTapCallback? onclick) {
    return GestureDetector(
      onTap: onclick,
      child: Container(
        height: 50,
        color: wTheme.listItemBg,
        padding: EdgeInsets.only(left: 16, right: 16),
        child: Row(
          children: [
            Image.asset(
              img,
              width: 20,
              height: 20,
              fit: BoxFit.fill,
            ),
            Padding(padding: EdgeInsets.only(left: 8)),
            Expanded(
              child: Text(
                title,
                style: TextStyle(fontSize: 16, color: wTheme.text1),
              ),
              flex: 1,
            ),
            Image.asset(
              R.imagesIcArraw,
              width: 10,
              height: 10,
            )
          ],
        ),
      ),
    );
  }


  // _getUserInfo() async {
  //   ResultData result = await HttpManager.instance()
  //       .get("lg/coin/list/${_pageIndex}/json", withLoading: _pageIndex == 0);
  //
  //
  //   if (result.success) {
  //     setState(() {
  //
  //     });
  //   } else {
  //     Fluttertoast.showToast(msg: result.msg);
  //   }
  // }
}
